package org.dromara.common.core.utils;

import org.dromara.common.core.exception.ServiceException;

import java.util.List;

/**
 * @Description 变量校验工具
 * @Author ZhouWenTao
 * @Date 2023/6/26 19:14
 */
public abstract class AssertUtils {
    /**
     * 判断真假值
     * @param flag
     * @param message
     */
    public static void isTrue(boolean flag, String message) {
        if (!flag) {
            throw new ServiceException(message);
        }
    }

    /**
     * true时拦截
     * @param flag
     * @param message
     */
    public static void notTrue(boolean flag, String message) {
        if (flag) {
            throw new ServiceException(message);
        }
    }

    public static void notNull(Object object, String message) {
        try {
            if (object == null) {
                throw new ServiceException(message);
            }
        }catch (Exception e){
            throw new ServiceException(message);
        }
    }

    /**
     * 不为空时抛出
     */
    public static void isEmpty(String str, String message) {
        try {
            if (StringUtils.isNotBlank(str)) {
                throw new ServiceException(message);
            }
        }catch (Exception e){
            throw new ServiceException(message);
        }
    }

    /**
     * 不为NULL时抛出异常
     */
    public static void isNull(Object obj, String message) {
        try {
            if (null != obj) {
                throw new ServiceException(message);
            }
        }catch (Exception e){
            throw new ServiceException(message);
        }
    }

    public static void notEmpty(String str, String message) {
        if (StringUtils.isBlank(str)) {
            throw new ServiceException(message);
        }
    }

    /**
     * 校验Array 不可为空/不可空数据
     * @param list
     * @param message
     */
    public static void hasSize(List list,String message){
        if (list==null || list.size()==0) {
            throw new ServiceException(message);
        }
    }

    /**
     * 校验Array 不可为空/不可空数据
     * @param list
     * @param message
     */
    public static void hasSize(Object[] list,String message){
        if (list==null || list.length==0) {
            throw new ServiceException(message);
        }
    }

    /**
     * 判断字符串长度
     * @param o 判读对象
     * @param len 字符串长度，不可超过
     * @param message 异常提示
     */
    public static void checkLength(Object o,Integer len,String message){
        notNull(o,"<"+message+">判断之前参数不可空!");
        if (o.toString().length()>len) {
            throw new ServiceException(message);
        }
    }
}
